package com.sunyur.tailor.channel.service.inner.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelListResultUtil;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.user.param.UserRoleSyncParam;
import com.sunyur.tailor.channel.service.inner.user.param.UserSyncParam;
import com.sunyur.tailor.channel.service.inner.user.service.SunyurUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @ClassName SunyurUserServiceImpl
 * @Description 商城人员服务
 * @Author yu.chen
 * @Date 2021/4/29 3:51
 * @Version v1.0
 */
@Slf4j
@Service("sunyurUserService")
public class SunyurUserServiceImpl implements SunyurUserService {
    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    /**
     * 人员信息同步
     */
    @Value("${sunyur.api.syncUserInfoList}")
    private String syncUserInfoList;

    /**
     * 用户角色同步
     */
    @Value("${sunyur.api.syncUserRoleList}")
    private String syncUserRoleList;

    /**
     * @param userSyncParamList
     * @return com.sunyur.tailor.channel.common.middle.base.ChannelResult
     * @description 人员信息批量同步主线
     * @author yu.chen
     * @date 2021/4/29 3:57
     * @Version v1.0
     */
    @Override
    public ChannelResult syncUserList(List<UserSyncParam> userSyncParamList){
        log.info("SunyurUserServiceImpl syncUserList param is {}", JSON.toJSONString(userSyncParamList));
        if (CollectionUtils.isEmpty(userSyncParamList)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), MiddleResultEnum.FAILED.getValue());
        }
        // 调用主线查询采购单行信息接口
        SunyurResult<JSONArray> sunyurResult = sunyurClient.execPost(syncUserInfoList, JSON.toJSONString(userSyncParamList), null);
        if (StringUtils.isEmpty(sunyurResult.getErrorMessage()) && CollectionUtils.isEmpty(sunyurResult.getContent())) {
            return ChannelResultUtil.success(Boolean.TRUE);
        }
        log.error("sync user to sunyur failed：{}", JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage() + sunyurResult.getContent());
    }

    @Override
    public ChannelResult syncUserRoleList(List<UserRoleSyncParam> userRoleSyncParamList){
        log.info("SunyurUserServiceImpl syncUserRoleList param is {}", JSON.toJSONString(userRoleSyncParamList));
        if (CollectionUtils.isEmpty(userRoleSyncParamList)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), MiddleResultEnum.FAILED.getValue());
        }
        // 调用主线查询采购单行信息接口
        SunyurResult<JSONArray> sunyurResult = sunyurClient.execPost(syncUserRoleList, JSON.toJSONString(userRoleSyncParamList), null);
        if (StringUtils.isEmpty(sunyurResult.getErrorMessage()) && CollectionUtils.isEmpty(sunyurResult.getContent())) {
            return ChannelResultUtil.success(Boolean.TRUE);
        }
        log.error("SunyurUserServiceImpl syncUserRoleList failed：{}", JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage() + sunyurResult.getContent());
    }
}
